package com.gxa.admin.controller;

import com.gxa.common.exception.LcException;
import com.gxa.common.vo.ResultVo;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @program: microloans_system
 * @description: 全局异常处理类
 * @author: LC
 * @create: 2021-12-29 16:05
 */

@ControllerAdvice
public class GlobalExceptionController {


    @ExceptionHandler(LcException.class)
    @ResponseBody
    public ResultVo ioException(LcException exception){
        ResultVo r=new ResultVo();
        r.setCode(exception.getCode());
        r.setMsg(exception.getMessage());
        return r;
    }


    @ExceptionHandler(UnknownAccountException.class)
    @ResponseBody
    public ResultVo unknownAccountException(){
        ResultVo r=new ResultVo();
        r.setCode(400);
        r.setMsg("用户名错误");
        return r;
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    @ResponseBody
    public ResultVo incorrectCredentialsException(){
        ResultVo r=new ResultVo();
        r.setCode(400);
        r.setMsg("用户密码错误");
        return r;
    }

    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    public ResultVo unauthorizedException(){
        ResultVo r=new ResultVo();
        r.setCode(400);
        r.setMsg("角色错误");
        return r;
    }

    @ExceptionHandler(LockedAccountException.class)
    @ResponseBody
    public ResultVo lockedAccountException(){
        ResultVo r=new ResultVo();
        r.setCode(400);
        r.setMsg("账号被锁");
        return r;
    }

    @ExceptionHandler(AuthenticationException.class)
    @ResponseBody
    public ResultVo authenticationException(){
        ResultVo r=new ResultVo();
        r.setCode(400);
        r.setMsg("认证错误");
        return r;
    }

    @ExceptionHandler(AuthorizationException.class)
    @ResponseBody
    public ResultVo authorizationException(){
        ResultVo r=new ResultVo();
        r.setCode(400);
        r.setMsg("未授权");
        return r;
    }

}